<template>
  <div class="page">
    <div class="title">
      点击查看问卷题目
      <el-button type="primary">
        新增问卷
      </el-button>
    </div>
    <div class="content">
      <el-card class="box-card" v-for="item in papers" :key="item.id">
        <div class="flex">
          <div>
            {{ item.title }}
          </div>
          <div>
            <el-button @click="getPaperQuestion(item.paperId)">
              修改问题
            </el-button>
            <el-button type="danger" @click="deletePaper(item.paperId)">
              删除问卷
            </el-button>
          </div>
        </div>
      </el-card>
    </div>

    <el-pagination class="pagination" @current-change="pageNoChange" :current-page.sync="page" :page-size="10"
      layout="total, prev, pager, next" :total="total">
    </el-pagination>

    <div v-if="detailVisible" class="detail">
      <el-button type="primary" @click="addQuestino">新增问题</el-button>
      <el-table :data="paperQuestions" style="width: 100%;" height="calc(100% - 40px)">
        <el-table-column type="index" width="50">
        </el-table-column>
        <el-table-column prop="name" label="题目" min-width="300">
        </el-table-column>
        <el-table-column prop="questionType" label="类型" min-width="100">
        </el-table-column>
        <el-table-column prop="value" label="选项" min-width="300">
        </el-table-column>
        <el-table-column prop="value" label="remark" min-width="200">
        </el-table-column>
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="{row}">
            <el-button type="primary" @click="changeQuestion(row)">
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="form.questionId ? '修改' : '新增'" :visible.sync="dialogVisible" width="600px" :modal="false"
        :before-close="handleClose">
        <el-form ref="questionForm" :model="form" label-position="top">
          <el-form-item label="题目">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.questionType" style="width: 100%;">
              <el-option label="单选" value="radio"></el-option>
              <el-option label="多选" value="checkbox"></el-option>
              <el-option label="填空" value="input"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选项(填空时为空)">
            <el-input type="textarea" v-model="form.value" placeholder="选项之间用|分隔"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPapers, deletePaper, getPaperQuestion } from '@/api/official/official'
export default {
  name: 'InternetHospitalMangePaperManage',

  data() {
    return {
      page: 1,
      total: 0,
      papers: [],
      paperQuestions: [],
      detailVisible: false,
      dialogVisible: false,
      form: {},
    };
  },

  mounted() {
    this.getPapers();
  },

  methods: {
    pageNoChange(page) {
      this.page = page;
      this.getPapers();
    },
    getPapers() {
      getPapers({ page: this.page }).then(res => {
        this.papers = res.content
        this.total = res.totalElements;
      });
    },
    deletePaper(id) {
      // deletePaper({paperid: id}).then(res => {
      deletePaper([id]).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.getPapers();
      })
    },
    getPaperQuestion(id) {
      this.detailVisible = true
      getPaperQuestion({ paperid: id }).then(res => {
        this.paperQuestions = res.content
      })
    },
    addQuestino() {
      this.dialogVisible = true
      this.form = {}
    },
    changeQuestion(row) {
      this.dialogVisible = true
      this.form = JSON.parse(JSON.stringify(row))
    },
    submit() {
      if (this.form.questionId) {
        questionsUpdate(this.form).then(res => {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.dialogVisible = false
          this.getPaperQuestion(this.form.paperId)
        })
      } else {
        questionsAdd(this.form).then(res => {
          this.$message({
            type: 'success',
            message: '添加成功'
          });
          this.dialogVisible = false
          this.getPaperQuestion(this.form.paperId)
        })
      }
    }
  },
};
</script>

<style scoped>
.page {
  min-height: calc(100vh - 84px);
  padding: 24px;
  position: relative
}

.title {
  font-size: 24px;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.box-card {
  cursor: pointer;
  margin-bottom: 24px;
}

.content {
  height: calc(100vh - 226px);
  min-height: 500px;
  overflow: auto;
}

.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  width: 100%;
  z-index: 1000;
  padding: 24px;
  background-color: #FFF;
}
</style>